#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "def.h"
#include "nrio.h"
#include "nrarith.h"
#include "nralloc.h"

void sobel(char *filename, char *origin)
{
	long nrh,nrl,nch,ncl;
	byte **I;
	byte **J;
	int **bufferx;
	int **buffery;
	long i,j;
	int maskx[3][3];
	int masky[3][3];
	int normtot=0, moy_norm=0, tot=0;
	int nb_pix_blanc=0, nb_pix_tot=0;
	
	/* Gradient vertical. */
	masky[0][0] = -1;
	masky[1][0] = -2;
	masky[2][0] = -1;
	
	masky[0][1] = 0;
	masky[1][1] = 0;
	masky[2][1] = 0;
	
	masky[0][2] = 1;
	masky[1][2] = 2;
	masky[2][2] = 1;
	
	/*Gradient horizontal. */
	maskx[0][0] = -1;
	maskx[1][0] = 0;
	maskx[2][0] = 1;
	
	maskx[0][1] = -2;
	maskx[1][1] = 0;
	maskx[2][1] = 2;
	
	maskx[0][2] = -1;
	maskx[1][2] = 0;
	maskx[2][2] = 1;
		
	I=LoadPGM_bmatrix(filename,&nrl,&nrh,&ncl,&nch);
	
	J=bmatrix(nrl,nrh,ncl,nch);

	bufferx = imatrix(nrl, nrh, ncl, nch);
	buffery = imatrix(nrl, nrh, ncl, nch);
	
	printf("Image chargée. \n");

	for(i=ncl+1;i<nch;i++)
	{
		printf("première boucle.\n");
		for(j=nrl+1;j<nrh;j++)
		{
			//printf("Traitement (%d, %d)... \n", i, j);

			bufferx[j][i] += maskx[0][0]*I[j-1][i-1];
			bufferx[j][i] += maskx[0][1]*I[j][i-1];
			bufferx[j][i] += maskx[0][2]*I[j+1][i-1];
			
			
			bufferx[j][i] += maskx[1][0]*I[j-1][i];
			bufferx[j][i] += maskx[1][1]*I[j][i];
			bufferx[j][i] += maskx[1][2]*I[j+1][i];
			
			bufferx[j][i] += maskx[2][0]*I[j-1][i+1];
			bufferx[j][i] += maskx[2][1]*I[j][i+1];
			bufferx[j][i] += maskx[2][2]*I[j+1][i+1];
			
		//	bufferx[j][i] /= 4;
			
			buffery[j][i] += masky[0][0]*I[j-1][i-1];
			buffery[j][i] += masky[0][1]*I[j][i-1];
			buffery[j][i] += masky[0][2]*I[j+1][i-1];
			
			
			buffery[j][i] += masky[1][0]*I[j-1][i];
			buffery[j][i] += masky[1][1]*I[j][i];
			buffery[j][i] += masky[1][2]*I[j+1][i];
			
			buffery[j][i] += masky[2][0]*I[j-1][i+1];
			buffery[j][i] += masky[2][1]*I[j][i+1];
			buffery[j][i] += masky[2][2]*I[j+1][i+1];
			
			//buffery[j][i] /= 4;
			
			J[j][i] = sqrt((bufferx[j][i]*bufferx[j][i]) + (buffery[j][i]*buffery[j][i]));
			normtot+=J[j][i];
			tot++;
		}
	}
	moy_norm = normtot/tot;
	
	sprintf(filename, "%s_sobel.pgm", filename);
	SavePGM_bmatrix(J,nrl,nrh,ncl,nch,filename);
	
	printf("Transformation done. \n");
	
	free_bmatrix(J,nrl,nrh,ncl,nch);
	printf("J libérée. \n");
	free_bmatrix(I,nrl,nrh,ncl,nch);
	printf("I libérée. \n");
	free_imatrix(buffery,nrl,nrh,ncl,nch);
	printf("Buffery libérée. \n");
	free_imatrix(bufferx,nrl,nrh,ncl,nch);
	printf("Bufferx libérée. \n");
	
	sprintf(origin, "%s.txt", origin);
	printf("Ouverture %s \n", origin);
	FILE *f=fopen(origin, "a");
	fprintf(f, "%d\n", moy_norm);

}


/*
void moyenneur(void)
{
	long nrh,nrl,nch,ncl;
	byte **I;
	
	I=LoadPGM_bmatrix("cubesx3.pgm",&nrl,&nrh,&ncl,&nch);
	byte **J;
	long i,j;
	int mask = 1, buffer = 0;
	
	J=bmatrix(nrl,nrh,ncl,nch);
	
	printf("cubesx3.pgm chargée. \n");
	
	
	
	for(i=ncl+1;i<nch;i++)
	{
		printf("première boucle. ");
		for(j=nrl+1;j<nrh;j++)
		{
			printf("begin (%d, %d). \n", i, j);
			buffer += mask*I[j-1][i];
			buffer += mask*I[j][i-1];
			buffer += mask*I[j+1][i];
			buffer += mask*I[j][i+1];
			buffer += mask*I[j-1][i-1];
			buffer += mask*I[j-1][i+1];
			buffer += mask*I[j+1][i-1];
			buffer += mask*I[j+1][i+1];
			buffer /= 9;
			J[j][i] =buffer;
			
		}
	}
	
	
	SavePGM_bmatrix(J,nrl,nrh,ncl,nch,"cubesx3_moyennee.pgm");
	
	printf("Image sauvegardée.\n");
	
	free_bmatrix(J,nrl,nrh,ncl,nch);
	printf("Matrice J libérée.\n");
	
	free_bmatrix(I,nrl,nrh,ncl,nch);	
	printf("Matrice I libérée. \n");

}*/


void binarize(char *filename)
{
	long nrh,nrl,nch,ncl;
	byte **I;
	long i,j;
	char name[100];
	int seuil = 125;

	printf("BINARIZE de %s\n", filename);
	
	I=LoadPGM_bmatrix(filename,&nrl,&nrh,&ncl,&nch);

	for(i=ncl+1;i<nch;i++)
	{
		//printf("première boucle.\n");
		for(j=nrl+1;j<nrh;j++)
		{
			if(I[i][j]>seuil)
				I[i][j]=254;
			else I[i][j]=0;	
		}
	}	
	
	sprintf(name, "%s_binar.pgm", filename);
	SavePGM_bmatrix(I,nrl,nrh,ncl,nch,name);
	
	printf("Transformation done. \n");
	
	free_bmatrix(I,nrl,nrh,ncl,nch);
	printf("I libérée. \n");
	
	printf("FIN BINARIZE\n");
}



int main(int argc, char *argv[])
{
	char filename[100];
	char origin[100];
	sprintf(origin, "%s", argv[1]);
	binarize(origin);
	sprintf(filename, "%s_binar.pgm", argv[1]);

	sobel(filename, origin);

	
	return 1;

}

